package cn.fjc920.siaa.activitys

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.icu.text.ListFormatter.Width
import android.os.Bundle
import android.view.WindowManager
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import cn.fjc920.siaa.R
import cn.fjc920.siaa.activityutils.ActivityCollector
import cn.fjc920.siaa.activityutils.BaseActivity
import cn.fjc920.siaa.activityutils.SharedPreferenceKeys
import cn.fjc920.siaa.activityutils.ThemeProvider
import kotlinx.android.synthetic.main.settings_activity.*


class SettingsActivity : BaseActivity() {

    private lateinit var context: Context

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)

        init(savedInstanceState)
        initClick()
    }

    private fun init(savedInstanceState: Bundle?) {
        context = this
        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, SettingsFragment())
                .commit()
        }
        setSupportActionBar(settings_toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    @SuppressLint("CommitPrefEdits", "InflateParams", "MissingInflatedId")
    private fun initClick() {
        settings_toolbar.setNavigationOnClickListener {
            finish()
        }

        btn_exitLogin.setOnClickListener {

            val view = layoutInflater.inflate(R.layout.dialog_signout,null)
            val alertDialog = AlertDialog.Builder(context).run {
                setView(view)
                create()
                show()
            }

            alertDialog.window?.apply {
                setBackgroundDrawableResource(R.drawable.drawable_round_edge)
            }

            view.findViewById<Button>(R.id.btn_Cancel).setOnClickListener {
                alertDialog.dismiss()
            }
            view.findViewById<Button>(R.id.btn_Sure).setOnClickListener {
                getSharedPreferences("userData", Context.MODE_PRIVATE).edit {
                    remove("userName")
                    remove("password")
                }
                ActivityCollector.finishAll()
                LoginActivity.actionStart(context)
            }
        }
    }

    class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)

            val theme = findPreference<Preference>(SharedPreferenceKeys.THEME)!!
            theme.onPreferenceChangeListener =
                Preference.OnPreferenceChangeListener setOnPreferenceChangeListener@{ _, newValue ->
                    if (newValue is String) {
                        val provider = ThemeProvider(requireContext())
                        val newTheme = provider.getTheme(newValue)
                        AppCompatDelegate.setDefaultNightMode(newTheme)
                        return@setOnPreferenceChangeListener true
                    }
                    false
                }
        }
    }

    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, SettingsActivity::class.java)
            context.startActivity(intent)
        }
    }
}